package controller;

import Pojo.User;
import Pojo.Userlist;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import service.CheckoutService;
import service.UserlistService;

@Controller
@RequestMapping({"/checkout"})
public class CheckoutController
{

  @Autowired
  private CheckoutService checkoutService;

  @Autowired
  private UserlistService userlistService;

  @RequestMapping({"/getallcheckout"})
  public String getallcheckout(Model model, @RequestParam(required=false, defaultValue="1") Integer page, @RequestParam(required=false, defaultValue="5") Integer pageSize)
  {
    PageHelper.startPage(page.intValue(), pageSize.intValue());
    List checkout = this.checkoutService.getallcheckout();
    PageInfo p = new PageInfo(checkout);
    model.addAttribute("p", p);
    model.addAttribute("checkout", checkout);
    model.addAttribute("mainPage", "checkout.jsp");
    return "admin/main1";
  }

  @RequestMapping({"/deletecheckout"})
  public String deletecheckout(Integer id) {
    this.checkoutService.deletecheckout(id);
    return "redirect:/checkout/getmycheckout.action";
  }

  @RequestMapping({"/admindeletecheckout"})
  public String admindeletecheckout(Integer id) {
    this.checkoutService.deletecheckout(id);
    return "redirect:/checkout/getallcheckout.action";
  }

  @RequestMapping({"/getmycheckout"})
  public String getmycheckout(Model model, HttpSession httpSession, @RequestParam(required=false, defaultValue="1") Integer page, @RequestParam(required=false, defaultValue="5") Integer pageSize)
  {
    User user1 = (User)httpSession.getAttribute("user");
    Userlist userlist = this.userlistService.findhasuserlist(user1.getId());
    PageHelper.startPage(page.intValue(), pageSize.intValue());
    List list = this.userlistService.getmycheckout(userlist.getId());
    PageInfo p = new PageInfo(list);
    model.addAttribute("p", p);
    model.addAttribute("userlistcheck", list);
    model.addAttribute("mainPage", "mycheckout.jsp");
    return "zuke/main";
  }
}